﻿@{
    ViewBag.Title = "Decorator";
    Layout = "~/Views/Shared/_Page.cshtml";
}

<h2>Decorator</h2>

<script type="text/javascript">
    function Sale(price) {
        this.price = price || 100;
    }
    Sale.prototype.getPrice = function () {
        return this.price;
    };
    Sale.decorators = {};

    Sale.decorators.fedtax = {
        getPrice: function () {
            var price = this.uber.getPrice();
            price += price * 5 / 100;
            return price;
        }
    };

    Sale.decorators.quebec = {
        getPrice: function () {
            var price = this.uber.getPrice();
            price += price * 7.5 / 100;
            return price;
        }
    };

    Sale.decorators.money = {
        getPrice: function () {
            return "$" + this.uber.getPrice().toFixed(2);
        }
    };

    Sale.decorators.cdn = {
        getPrice: function () {
            return "CDN$ " + this.uber.getPrice().toFixed(2);
        }
    };

    Sale.prototype.decorate = function (decorator) {
        var F = function () {},
            overrides = this.constructor.decorators[decorator],
            i, newobj;
        F.prototype = this;
        newobj = new F();
        newobj.uber = F.prototype;

        for (i in overrides) {
            if (overrides.hasOwnProperty(i)) {
                newobj[i] = overrides[i];
            }
        }

        return newobj;
    };

    window.onload = function () {
        var sale = new Sale(100);                   // the price is 100 dollars
        ZEUS.DEBUG.log("Fed Tax: " + sale.decorate('fedtax').getPrice());    // add federal tax
        ZEUS.DEBUG.log("Que Bec: " + sale.decorate('quebec').getPrice());    // add provincial tax
        ZEUS.DEBUG.log("Money:" + sale.decorate('money').getPrice());     // format like money
        ZEUS.DEBUG.log("Price: " + sale.getPrice());            // "$112.88"
    }
</script>
